package com.toonyoo.eurekafeign;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.toonyoo.eurekafeign.apis.HelloInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
@RestController
public class EurekaFeignApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaFeignApplication.class, args);
	}

	@Autowired
	private HelloInterface helloInterface;

	@Value("${netty.port}")
	private int port;

	@Bean(initMethod = "runNetty")
	public NettyServer nettyServer(){
		return new NettyServer(port);
	}

	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

	@Autowired
	private RestTemplate restTemplate;

	@RequestMapping(value = "/test")
	public String test(String name){
		return helloInterface.sayHiFromClientOne(name);
	}

	@RequestMapping(value = "/test2")
	@HystrixCommand(fallbackMethod = "hiError")
	public String test2(String name){
		return restTemplate.getForObject("http://producer/sayHello?name="+name, String.class);
	}

	public String hiError(String name) {
		return "hi,"+name+",sorry,error!";
	}
}
